package com.company;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {

    static int fileNumber = 10;
    static int fileSize = 1024 * 1024 * 1024;
    static String content = getRandomString(fileSize);

    public static void main( String[] args ) {
        executeThreadTask();
        executeSingleTask();
    }

    public static void executeSingleTask() {
        long startTime = System.currentTimeMillis();
        try {
            SingleTask.task(fileNumber, content);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.print("single: ");
        System.out.println(System.currentTimeMillis() - startTime);
    }

    public static void executeThreadTask() {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(10, 16, 10,
                TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(50));

        long startTime = System.currentTimeMillis();
        int threadNumber = 10;
        for(int i = 0; i < threadNumber; i++) {
            pool.execute(new ThreadTask(fileNumber / threadNumber, content));
        }

        pool.shutdown();
        while(!pool.isTerminated()){}
        System.out.print("thread: ");
        System.out.println(System.currentTimeMillis() - startTime);
    }

    //length用户要求产生字符串的长度
    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < length; i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }



}


